package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/mimacontroller")
public class MiMacontroller {

	private static final Logger logger = Logger.getLogger(MiMacontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	//普通用户密码找回
	@RequestMapping("/ptzh")
	@ResponseBody
	
	public String puzh(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
		//读取配置文件路劲
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Map remap = new HashMap();
		BODY body=new BODY();
		String Key="";
		String interfaceUrl = ReadConfig.readValue();
		if(sessionIn==null){
			body.setPassWd("123456");
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("LOGUserName", "admin");
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
			List jx = (List) mx2.get("RETDATA");
			Map m = (Map) jx.get(0);
			Key += m.get("OpenKey").toString();		
			
	}else if(sessionIn.getKEY()==null){
		body.setPassWd("123456");
		remap.put("AUTHORITY", "YBGJJ100");
		// 接口参数
		remap.put("LOGUserName", "admin");
		remap.put("BODY", body);
		String json3 = JSONUtil.ToJsongString2(remap);
		// 请求
		String result2 = HttpRequest.doPost(interfaceUrl, json3);
		logger.info("接口返回内容:" + result2);
		Map retMap2 = JSONUtil.toMap(result2);
		Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
		List jx = (List) mx2.get("RETDATA");
		Map m = (Map) jx.get(0);
		Key +=m.get("OpenKey").toString();	
		}
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ105");
		//接口参数
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",Key);
		b.setIDCardNumber(sfzh);
		b.setGRZH(grzh);
		b.setUserName(yhmc);
		b.setCategory(Category);
		reqMap.put("BODY",b);
		String result=opk.getOpenKey(reqMap, session);
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			if(resMap.get("RETURNCODE").equals("100")){
				
				map.put("code", 0); 
				map.put("msg", null);
				map.put("data",resMap.get("body"));
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
	//高级用户密码找回
		@RequestMapping("/gjzh")
		@ResponseBody
		
		public String gjzh(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
			//读取配置文件路劲
			SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			Map remap = new HashMap();
			BODY body=new BODY();
			String Key="";
			String interfaceUrl = ReadConfig.readValue();
			if(sessionIn==null){
				body.setPassWd("123456");
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		
				
		}else if(sessionIn.getKEY()==null){
			body.setPassWd("123456");
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("LOGUserName", "admin");
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
			List jx = (List) mx2.get("RETDATA");
			Map m = (Map) jx.get(0);
			Key +=m.get("OpenKey").toString();	
			}
			
			Map reqMap = new HashMap();
			BODY b=new BODY();
			OpenKey opk=new OpenKey();
			//接口编码
			reqMap.put("AUTHORITY", "YBGJJ105");
			//接口参数
			reqMap.put("LOGUserName","admin");
			reqMap.put("OpenKey",Key);
			b.setIDCardNumber(sfzh);
			b.setKAHAO(grzh);
			b.setUserName(yhmc);
			b.setCategory(Category);
			reqMap.put("BODY",b);
			String result=opk.getOpenKey(reqMap, session);
			logger.info("接口返回内容:"+result);
			if(result != null){
				Map resMap = JSONUtil.toMap(result);
				if(resMap.get("RETURNCODE").equals("100")){
					
					map.put("code", 0); 
					map.put("msg", null);
					map.put("data", resMap.get("body"));
					list.add(resMap);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}else{
					map.put("code", 1); 
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}
			}
			return null;
		}
		//单位用户密码找回
				@RequestMapping("/dwzh")
				@ResponseBody
				
				public String dwzh(HttpServletRequest request,String jbrzjhm,String dwmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
					//读取配置文件路劲
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
					Map remap = new HashMap();
					BODY body=new BODY();
					String Key="";
					String interfaceUrl = ReadConfig.readValue();
					if(sessionIn==null){
						body.setPassWd("123456");
						remap.put("AUTHORITY", "YBGJJ100");
						// 接口参数
						remap.put("LOGUserName", "admin");
						remap.put("BODY", body);
						String json3 = JSONUtil.ToJsongString2(remap);
						// 请求
						String result2 = HttpRequest.doPost(interfaceUrl, json3);
						logger.info("接口返回内容:" + result2);
						Map retMap2 = JSONUtil.toMap(result2);
						Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
						List jx = (List) mx2.get("RETDATA");
						Map m = (Map) jx.get(0);
						Key += m.get("OpenKey").toString();		
						
				}else if(sessionIn.getKEY()==null){
					body.setPassWd("123456");
					remap.put("AUTHORITY", "YBGJJ100");
					// 接口参数
					remap.put("LOGUserName", "admin");
					remap.put("BODY", body);
					String json3 = JSONUtil.ToJsongString2(remap);
					// 请求
					String result2 = HttpRequest.doPost(interfaceUrl, json3);
					logger.info("接口返回内容:" + result2);
					Map retMap2 = JSONUtil.toMap(result2);
					Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
					List jx = (List) mx2.get("RETDATA");
					Map m = (Map) jx.get(0);
					Key +=m.get("OpenKey").toString();	
					}
					Map reqMap = new HashMap();
					BODY b=new BODY();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ105");
					//接口参数
					reqMap.put("LOGUserName","admin");
					reqMap.put("OpenKey",Key);
					b.setIDCardNumber(jbrzjhm);
					b.setUserName(dwmc);
					b.setCategory(Category);
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							
							map.put("code", 0); 
							map.put("msg", null);
							map.put("data", resMap.get("body"));
							list.add(resMap);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}
					}
					return null;
				}
				//个人登录后里的修改密码
				@RequestMapping("/text")
				@ResponseBody
				
				public String gr(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
					//读取配置文件路劲
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			       
					String interfaceUrl = ReadConfig.readValue();
					Map reqMap = new HashMap();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ155");
					//接口参数
					reqMap.put("LOGUserName","admin");
					reqMap.put("OpenKey",sessionIn.getKEY());
					BODY b=new BODY();
					b.setUserName(jpassword);
					b.setPASSWORD(xpassword);
					b.setCategory("1");
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							
							map.put("code", 0); 
							map.put("msg", null);
							map.put("data",resMap.get("body"));
							list.add(resMap);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}else{
							map.put("code", 1);
							map.put("CW", resMap.get("RETURNDESC").toString());
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
							return null;
						}
					}
					return null;
				}
				//单位登录后里的修改密码
				@RequestMapping("/dwxg")
				@ResponseBody
				
				public String dw(HttpServletRequest request,String sfzh,String grzh,String yhmc,String Category,HttpSession session, HttpServletResponse response,String xpassword,String jpassword,String qpassword ) throws IOException{
					//读取配置文件路劲
					SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
			       
					String interfaceUrl = ReadConfig.readValue();
					Map reqMap = new HashMap();
					OpenKey opk=new OpenKey();
					//接口编码
					reqMap.put("AUTHORITY", "YBGJJ155");
					//接口参数
					reqMap.put("LOGUserName","admin");
					reqMap.put("OpenKey",sessionIn.getKEY());
					BODY b=new BODY();
					b.setUserName(jpassword);
					b.setPASSWORD(xpassword);
					b.setCategory("3");
					reqMap.put("BODY",b);
					String result=opk.getOpenKey(reqMap, session);
					logger.info("接口返回内容:"+result);
					if(result != null){
						Map resMap = JSONUtil.toMap(result);
						if(resMap.get("RETURNCODE").equals("100")){
							
							map.put("code", 0); 
							map.put("msg", null);
							map.put("data",resMap.get("body"));
							list.add(resMap);
							response.setHeader("Content-type", "text/html;charset=UTF-8");
							response.setCharacterEncoding("UTF-8");
							String jsonString = JSONUtil.ToJsongString2(map);
							PrintWriter pw = response.getWriter();
							pw.write(jsonString);
						}
					}
					return null;
				}
}
